package com.thinkgsp.webapp.controller.rp;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.thinkgsp.jxc.model.RpContact;
import com.thinkgsp.jxc.model.RpContactBalance;
import com.thinkgsp.jxc.service.ContactBalanceService;
import com.thinkgsp.jxc.service.RpContactService;
import com.thinkgsp.webapp.util.ExportExcel;
import com.thinkgsp.webapp.util.ExtReturn;

/**
 * 应付款汇总表与明细表控制器
 * @author evan
 *
 */
@Controller
@RequestMapping(value="/rp/apaylist")
public class APayListController {
	@Autowired
	private RpContactService contactService;
	@Autowired
	private ContactBalanceService cbService;
	
	@RequestMapping(value="/summary.do")
	@ResponseBody
	public Object getSummary(@RequestParam("begYear") Integer begYear,
							 @RequestParam("begPeriod") Integer begPeriod,
							 @RequestParam("endYear") Integer endYear,
							 @RequestParam("endPeriod") Integer endPeriod,
							 @RequestParam("custId") Integer custId,
							 @RequestParam("start") int start,
							 @RequestParam("limit") int limit) {
		if(custId == null || custId == 0) {
			return cbService.getContactBalanceListForAccountsPayable(begYear, begPeriod, endYear, endPeriod,start,limit);
		} else {
			return cbService.getContactBalanceListForAccountsPayable(begYear, begPeriod, endYear, endPeriod, custId);
		}
	}
	
	@RequestMapping(value="/detail.do")
	@ResponseBody
	public Object getDetail(@RequestParam("begYear") Integer begYear,
							 @RequestParam("begPeriod") Integer begPeriod,
							 @RequestParam("endYear") Integer endYear,
							 @RequestParam("endPeriod") Integer endPeriod,
							 @RequestParam("custId") Integer custId) {
		if(custId == null || custId == 0) {
			return new ExtReturn("必须选择一个客商！");
		}
		return contactService.getRpContactListForAPay(begYear, begPeriod, endYear, endPeriod, custId);
	}
	
	@RequestMapping("/getAPaySummaryXLS.do")
    public void  getXLS(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		try{
			int begYear=Integer.parseInt(request.getParameter("begYear"));
			int begPeriod=Integer.parseInt(request.getParameter("begPeriod"));
			int endYear=Integer.parseInt(request.getParameter("endYear"));
			int endPeriod=Integer.parseInt(request.getParameter("endPeriod"));
			String cid=request.getParameter("custId");
			int custId=0;
			if(StringUtils.isNotEmpty(cid) && !"0".equals(cid)) {
				custId = Integer.parseInt(cid);
			} 
			
			response.setContentType("octets/stream");
	        response.addHeader("Content-Disposition","attachment;filename="+ new String(("应付款汇总导出").getBytes("GB2312"),"iso8859-1")+".xls");
	        List<RpContactBalance> cb=null;
	        if(custId == 0) {
	        	cb=this.cbService.getContactBalanceListForAccountsPayable(begYear, begPeriod, endYear, endPeriod);
	        }else{
	        	cb=this.cbService.getContactBalanceListForAccountsPayable(begYear, begPeriod, endYear, endPeriod,custId);
	        }
	        OutputStream out = response.getOutputStream();
	        ExportExcel<RpContactBalance> ex = new ExportExcel<RpContactBalance>();
	        HSSFWorkbook book= ex.exportExcelByFieldAnnotation(cb);
	        book.write(out);
	        
		}catch(Exception e){
			
		}		 
	}
	@RequestMapping("/getAPayDetailXLS.do")
    public void  getDetialXLS(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		try{
			int begYear=Integer.parseInt(request.getParameter("begYear"));
			int begPeriod=Integer.parseInt(request.getParameter("begPeriod"));
			int endYear=Integer.parseInt(request.getParameter("endYear"));
			int endPeriod=Integer.parseInt(request.getParameter("endPeriod"));
			String cid=request.getParameter("custId");
			int custId=0;
			if(StringUtils.isNotEmpty(cid) && !"0".equals(cid)) {
				custId = Integer.parseInt(cid);
			} 
			
			response.setContentType("octets/stream");
	        response.addHeader("Content-Disposition","attachment;filename="+ new String(("应付款明细导出").getBytes("GB2312"),"iso8859-1")+".xls");
	        List<RpContact> cb=null;
	        cb=contactService.getRpContactListForAPay(begYear, begPeriod, endYear, endPeriod, custId);
	        OutputStream out = response.getOutputStream();
	        ExportExcel<RpContact> ex = new ExportExcel<RpContact>();
	        HSSFWorkbook book= ex.exportExcelByFieldAnnotation(cb);
	        book.write(out);
	        
		}catch(Exception e){
			
		}		 
	}
}
